//
//  SyncDataModel.swift
//  Runner
//
//  Created by huimac on 2021/10/27.
//

import Foundation

@objc class SyncDataModel : BaseSynchronousData  {
    @objc var score: Double = 0.0
    @objc var age: Int = 0
    @objc var name: String?
    @objc var isBoy: Bool = false
    @objc var info: Dictionary<String, Any>?
    @objc var children: Array<ObjectModel>?
    @objc var object: ObjectModel?
    
    override func toJson() -> [String : Any] {
        return [
            "score": score,
            "age": age,
            "name": name ?? "",
            "isBoy": isBoy,
            "info":  info ?? [String: Any](),
            "children": children?.map({ (item) -> Dictionary<String, Any> in
                return item.toJson()
            }) ?? [Any](),
            "object": object?.toJson() ?? [Any](),
        ]
    }
    
    override func toObject(_ json: [String : Any]) -> Any {
        score = json["score"] as! Double
        age = json["age"] as! Int
        name = json["name"] as? String
        isBoy = json["isBoy"] as! Bool
        info = json["info"] as? [String: Any]
        children = (json["score"] as? Array<Dictionary>)?.map({ (item) -> Any? in
            return ObjectModel().toObject(item)
        }) as? Array<ObjectModel>
        object = ObjectModel().toObject((json["object"] as? [String : Any])!) as? ObjectModel
        return self
    }
}


@objc class ObjectModel : NSObject, ArchiveAbleObject {
    var uuid: String = ""
    @objc var name: String?
    @objc var age : Int = 0
    func toObject(_ json: [String : Any]) -> Any {
        name = json["name"] as? String
        age = json["age"] as! Int
        return self
    }
    func toJson() -> [String : Any] {
        return [
            "name":name ?? "",
            "age":age
        ]
    }
}
